package com.gohi.main.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import com.caucho.config.type.LongPrimitiveType;

/**
 * Description: <br>
 * 该类用于String的一些辅助操作。 <br>
 * 
 */
public class StringUtil {

    private static final Log LOG = LogFactory.getLog(StringUtil.class);
	public static final String LINE = new String(new byte[] { 0x0D, 0x0A });
	public static final NumberFormat NUM_FORMAT = NumberFormat.getCurrencyInstance(Locale.CHINA);
	public static long order=0;
	/**
	 * 左边填充
	 */
	public static int FILL_TYPE_LEFT = 0;

	/**
	 * 右边填充
	 */
	public static int FILL_TYPE_RIGHT = 1;

	/**
	 * 两端填充
	 */
	public static int FILL_TYPE_BOTH = 2;
	
	/**
	 * 元
	 */
	public static int MONEY_TYPE_YUAN = 1;
	
	/**
	 * 分
	 */
	public static int MONEY_TYPE_FEN = 2;
	
	/**
	 * 
	 * @param stime开始时间
	 * @param etime结束时间
	 * @param hour 套餐时间
	 * @param falg   0表示今天 1表不是今天不
	 * @return
	 * @throws ParseException
	 */
	public static String splitTime(String stime, String etime,int hour,int falg) throws ParseException {
		//System.err.println(stime);
		
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
		
		Date date = sf.parse(etime);
	    GregorianCalendar c = new GregorianCalendar();  
		c.setTime(date);
	    c.add(12,-hour);
		Date dt1=c.getTime(); //计算套餐结束时间 
		//表示这个时间是今天
		if(falg==0){
			//还要判断现在的时候 是否大于 上班的时间
			  if(sf.parse(stime).getTime()>sf.parse(StringUtil.getDate("HH:mm")).getTime()){
				  //System.err.println("max value");
					//System.err.println(stime);
					date=  sf.parse(stime);//计算套餐开始时间
			  }else{
				  	
					String hh = StringUtil.getDate("HH");
					String mm = StringUtil.getDate("mm");
					//大于整点
					if(Integer.parseInt(mm)>=30){
						mm="00";
						hh = Integer.parseInt(hh)+1+"";
					}else{
						mm="30";
					}
					date=  sf.parse(hh+":"+mm);//计算套餐开始时间
			  }
		
		}else{
			date=  sf.parse(stime);//计算套餐开始时间
		}

	    //先加上开始时间
		String reslut =sf.format(date)+"-";
		c.setTime(date);
		//从开始时间循环到结束时间 以半小时间隔
		while(!sf.format(c.getTime()).equals(sf.format(dt1))){
			c.setTime(c.getTime());
			c.set(Calendar.MINUTE,(c.get(Calendar.MINUTE) + 30));
			reslut=reslut+sf.format(c.getTime())+"-";
			//System.err.println(sf.format(c.getTime()));
		}
		return reslut;
	}
	
	
	
	/**
	 * 
	 * @param stime开始时间
	 * @param etime结束时间
	 * @param hour 套餐时间
	 * @return
	 * @throws ParseException
	 */
	public static String splitTime(String stime, String etime,int hour) throws ParseException {
		//System.err.println(stime);
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
		Date date = sf.parse(etime);
	    GregorianCalendar c = new GregorianCalendar();  
		c.setTime(date);
	    c.add(12,-hour);
		Date dt1=c.getTime(); //计算套餐结束时间 
		
		date=  sf.parse(stime);//计算套餐开始时间
	    //先加上开始时间
		String reslut =sf.format(date)+"-";
		c.setTime(date);
		//从开始时间循环到结束时间 以半小时间隔
		while(!sf.format(c.getTime()).equals(sf.format(dt1))){
			c.setTime(c.getTime());
			c.set(Calendar.MINUTE,(c.get(Calendar.MINUTE) + 30));
			reslut=reslut+sf.format(c.getTime())+"-";
			//System.err.println(sf.format(c.getTime()));
		}
		return reslut;
	}
	
	
	/**
	 * 产生12位随机数   用于支付完毕的消费密码
	 * @return
	 */
	public static String getpwd(){
		return  Math.round(Math.random()*9000+1000)+""+ Math.round(Math.random()*9000+1000)+""+ Math.round(Math.random()*9000+1000);
	}
	
	
	/***************************************************************************
	 * 检查是否包含某个字符串
	 * 
	 * @param set
	 * @param id
	 * @return
	 */
	public static boolean inSet(String set, String id) {
		return ("," + set + ",").indexOf("," + id + ",") != -1;
	}

	/**
	 * 字符串填充方法
	 * 
	 * @param source
	 *            原始字符串
	 * @param fillStr
	 *            填充字符串
	 * @param length
	 *            填充后的长度
	 * @param fillType
	 *            填充类型
	 * @return
	 */
	public static String fillString(String source, String fillStr, int length, int fillType) {
		int len = source.length();
		if (len >= length)
			return fillType == FILL_TYPE_LEFT ? source.substring(len - length) : source.substring(0, length);
		int i = 0;
		while (source.length() < length)
			source = ((fillType == FILL_TYPE_LEFT) || (fillType == FILL_TYPE_BOTH && (i++) % 2 == 0)) ? fillStr + source : source + fillStr;
		return (fillType == FILL_TYPE_RIGHT) ? source.substring(0, length) : source.substring(source.length() - length, source.length());
	}
	
	public static String fillStringSocket(String source, String fillStr, int length, int fillType, String code) throws Exception{
		int len = source.getBytes(code).length;
//		 int sourStrLen = sourStr.getBytes().length;
		if (len >= length)
			return fillType == FILL_TYPE_LEFT ? (new String(source.getBytes(code),len - length, len)) : (new String(source.getBytes(code), 0, length));
		int i = 0;
		while (source.getBytes(code).length < length)
			source = ((fillType == FILL_TYPE_LEFT) || (fillType == FILL_TYPE_BOTH && (i++) % 2 == 0)) ? fillStr + source : source + fillStr;
		
		return (fillType == FILL_TYPE_RIGHT) ? (new String(source.getBytes(code), 0, length)) : (new String(source.getBytes(code), source.getBytes(code).length-length, source.getBytes(code).length));
	}
	
	public static String getNextDay(String dateStr, int offset) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sf.parse(dateStr);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DATE,(c.get(Calendar.DATE) + offset));
		return sf.format(c.getTime());
	}

	/**
	 * 获取日期字符串
	 * 
	 * @param patten
	 *            格式化字符串
	 * @return
	 */
	public static String getDate(String patten) {
		SimpleDateFormat sf = new SimpleDateFormat(patten);
		
		return sf.format(new Date());
	}
	
	
	/**
	 * 获取时间搓	
	 * @return
	 */
	public static int getTimeSimple() {
		
		return (int) (System.currentTimeMillis()/1000); 
	}
	
	public static int getTimeSimple(Date dt) {
		System.err.println(dt.getTime());
		return (int) (dt.getTime()/1000); 
	}
	
	/**
	 * 把时间搓转换成时间
	 * @param simple  时间搓的值
	 * @param patten   格式化的时间
	 * @return
	 */
	public static String parentSimple(long simple, String patten) {
		return getDate(new Date(simple*1000),patten); 
	}

	/**
	 * 获取日期字符串
	 * 
	 * @param patten
	 *            格式化字符串
	 * @return
	 */
	public static String getDate(Date date, String patten) {
		SimpleDateFormat sf = new SimpleDateFormat(patten);
		return sf.format(date);
	}
	
	/**
	 * 当前日期加1
	 */
	public static String getNextDate(String dateStr) throws ParseException {
		return getNextDate(dateStr, 1);
	}
	
	/**
	 * 当前日期加offset值
	 */
	public static String getNextDate(String dateStr, int offset) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sf.parse(dateStr);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_YEAR,(c.get(Calendar.DAY_OF_YEAR) + offset));
		return sf.format(c.getTime());
	}
	
	/**
	 * 当前月加1
	 */
	public static String getNextMonth(String dateStr) throws ParseException {
		return getNextMonth(dateStr, 1);
	}
	
	/**
	 * 当前月加offset值
	 */
	public static String getNextMonth(String dateStr, int offset) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
		Date date = sf.parse(dateStr);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.MONTH,(c.get(Calendar.MONTH) + offset));
		return sf.format(c.getTime());
	}
    
    public static String getDaysInMonth(int year, int mon) {
        java.util.GregorianCalendar date = new java.util.GregorianCalendar(year, mon, 1);
        date.add(Calendar.DATE, -1);
        return String.valueOf(date.get(Calendar.DAY_OF_MONTH));
    }
	
	/**
	 * 当前年加1
	 */
	public static String getNextYear(String dateStr) throws ParseException {
		return getNextYear(dateStr, 1);
	}
	
	/**
	 * 当前年加offset值
	 */
	public static String getNextYear(String dateStr, int offset) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy");
		Date date = sf.parse(dateStr);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.YEAR,(c.get(Calendar.YEAR) + offset));
		return sf.format(c.getTime());
	}

	/**
	 * 返回解析后的Date对象
	 * 
	 * @param dateStr
	 * @param patten
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String dateStr, String patten) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat(patten);
		return sf.parse(dateStr);
	}

	/**
	 * 获取上一工作日期
	 * 
	 * @param patten
	 *            格式化字符串
	 * @param 日期
	 * @return
	 */
	public static String getPreWorkDay(String patten) {
		SimpleDateFormat sf = new SimpleDateFormat(patten);
		Calendar ca = Calendar.getInstance();
		int x = ca.get(Calendar.DAY_OF_WEEK);
		if (x == 1)
			ca.set(Calendar.DAY_OF_MONTH, ca.get(Calendar.DAY_OF_MONTH) - 2);
		else if (x == 2)
			ca.set(Calendar.DAY_OF_MONTH, ca.get(Calendar.DAY_OF_MONTH) - 3);
		else
			ca.set(Calendar.DAY_OF_MONTH, ca.get(Calendar.DAY_OF_MONTH) - 1);
		return sf.format(ca.getTime());
	}
    
    public static int getApart(String str1, String str2, String patten) throws Exception {
        SimpleDateFormat sdf=new SimpleDateFormat(patten);
        Calendar cal1=Calendar.getInstance();
        cal1.setTime(sdf.parse(str1));
        Calendar cal2=Calendar.getInstance();
        cal2.setTime(sdf.parse(str2));
        long l=cal2.getTimeInMillis()-cal1.getTimeInMillis();
        return new Long(l/(1000*60*60*24)).intValue();
    }

	/**
	 * 将List转化为指定字符串分割的长字符串
	 * 
	 * @param rs
	 * @param split
	 * @return
	 */
	public static String list2String(List rs, String split) {
        if (rs == null || rs.size() <= 0) {
            return null;
        }
        StringBuffer temp = new StringBuffer();
        for (int i = 0; i < rs.size(); i++) {
            temp.append(rs.get(i));
            temp.append(split);
        }
        StringBuffer result = new StringBuffer(temp.substring(0, temp.length()
                - split.length()));
        return result.toString();
	}
    
    /**
     * Description : 字符串转List <br>
     * Created on 2007-5-22 17:58:08 <br>
     * @param p_Param
     * @param p_Delim
     * @return
     */
    public static List String2List(String p_Param, String p_Delim) {
        if (p_Param == null || p_Param.trim().equals("")) {
            return null;
        }

        StringTokenizer st;
        if (p_Delim != null) {
            st = new StringTokenizer(p_Param, p_Delim);
        } else {
            st = new StringTokenizer(p_Param);
        }
        List result = new ArrayList();
        while (st.hasMoreTokens()) {
            result.add(st.nextToken());
        }
        return result;
    }

	/**
	 * 将字符串数据转化为指定字符串分割的长字符串
	 * 
	 * @param rs
	 * @param split
	 * @return
	 */
	public static String array2String(Object[] rs, String split) {
		StringBuffer result = new StringBuffer(rs[0].toString());
		for (int i = 1, n = rs.length; i < n; i++)
			result.append(split).append(rs[i]);
		return result.toString();
	}

	/**
	 * 获取货币格式
	 */
	public static String formatMoney(double money) {
		return NUM_FORMAT.format(money).replaceAll("￥", "");
	}

	private static String[] upperChars = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

	/**
	 * 将字符串中数字大写
	 * 
	 * @param str
	 * @return
	 */
	public static String numberUpper(String str) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			sb.append((c >= '0' && c <= '9') ? upperChars[c - '0'] : c + "");
		}
		return sb.toString();
	}
    
    public static String list2JoinStr(List param) {
        if (param == null || param.size() <= 0) {
            return "";
        }
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < param.size(); i++) {
            if (0 == i)
                result.append("-");
            result.append(param.get(i).toString().trim());
            result.append("-");
        }
        return result.toString();
    }
	
	public static String fen2yuan(String money)
	{
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0.00");
		money = df.format(new Double(money).doubleValue()/100);
		return money;
	}	
	
	public static String yuan2fen(String money)
	{
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0");
		money = df.format(new Double(money).doubleValue()*100);
		return money;
	}
	/**
	 * 给金额 添加 逗号
	 * @param moneyType
	 * @param money
	 * @return
	 */
	public static String showMoney(int moneyType, String money)
	{
		String formatMoney = null;
		if (money != null && !money.equalsIgnoreCase("")) {
			String prefix = null;
			if (money.startsWith("-")) {
				prefix = money.substring(0,1);
				money = money.substring(1);
			}
			if (moneyType == MONEY_TYPE_YUAN) {
				money = yuan2fen(money);
			}
			long v1,v2;
			money = money.trim();
			long moneyInt = Long.parseLong(money);
			v1 = moneyInt/100;
			v2 = moneyInt%100;
			StringBuffer result = new StringBuffer();
			result.append(".");
			if (v2 < 10)
				result.append("0");
			result.append(String.valueOf(v2));
			while (v1 >= 1000) {
				v2 = (v1 - 1000) % 1000;
				v1 = v1 / 1000;
				result.insert(0,fillString(String.valueOf(v2),"0",3,FILL_TYPE_LEFT));
				result.insert(0,",");
			}
			result.insert(0,String.valueOf(v1));
			if (prefix != null) {
				result.insert(0,prefix);
			}
			formatMoney = result.toString();
		}
		return formatMoney;
	}

	/**
	 * 比较两个日期相差的天数，date1-date2
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareDay(Date date1, Date date2) {
		return (int) ((date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24));
	}

	/**
	 * 获取Exception中的Message
	 * 
	 * @param e
	 * @return
	 */
	public static String getMessage(Exception e) {
		String ss = e.getMessage();
		int n = ss.indexOf(":");
		return ss.substring(n == -1 ? 0 : n + 1);
	}

	/**
	 * 用于解析模板文件，并替换其中的${}变量
	 * 
	 * @param var
	 * @param tempFile
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static String parseTempFile(Map var, String tempFile, String encoding) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile), encoding), 2048);
		StringBuffer sb = new StringBuffer();
		String s = null;
		while ((s = br.readLine()) != null)
			sb.append(s).append(LINE);
		br.close();
		String str = sb.toString();
		for (Iterator it = var.keySet().iterator(); it.hasNext();) {
			String key = it.next().toString();
			str = str.replaceAll("\\$\\{" + key + "\\}", var.get(key).toString());
		}
		// 替换所有未定义的${}
		str = str.replaceAll("\\$\\{.*\\}", "");
		return str;
	}

	/**
	 * 从字符串中取出一定长度的内容
	 * 
	 * @param e
	 * @return
	 */
	public static String prefixLenStr(String str, int len) {
		String result = null;
		if (str != null) {
			if (str.length() < len) {
				result = str;
			} else {
				result = str.substring(0, len);
			}
		}
		return result;
	}
    
    /**
     * Description : 过滤空字符串 <br>
     * Created on 2007-3-8 16:03:13 <br>
     * 
     * @param argStr
     * @return
     */
    public static String null2Blank(String inputStr) {
        if (inputStr == null || "null".equals(inputStr)) {
            return "";
        } else {
            return inputStr;
        }
    }
    
    /**
     * Description : 过滤空对象 <br>
     * Created on 2007-5-16 9:56:30 <br>
     * @param obj
     * @return
     */
    public static String null2Blank(Object obj) {
        if (obj == null) {
            return "";
        } else {
            return obj.toString();
        }
    }
    
    /**
     * Description : 返回_ID集合中的每个id加上引号，并以逗号隔开 <br>
     * Created on 2007-5-22 10:14:12 <br>
     * @param idList
     * @return
     */
    public static String getIdsForSql(List idList) {
        StringBuffer strBuf = new StringBuffer();
        if(idList!=null && idList.size()>0){
            for(int i=0; i<idList.size(); i++){
                strBuf.append((0==i)?"":",");
                strBuf.append("'").append(idList.get(i)).append("'");
            }
        }else{
            strBuf.append("''");
        }
        return strBuf.toString();
    }
	
	   /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is
     *  not empty and not null and not whitespace
     * @since 2.0
     */
    public static boolean isNotBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return false;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return true;
            }
        }
        return false;
    }
    
    
    /**
     * 按照模式  获取当前日期的字符串形式 如输入yyyy-MM-dd 获取今天为2007-05-22
     * @param pattern
     * @return
     */
    public static String getFormatDate(String pattern){
    	DateFormat df=new SimpleDateFormat(pattern);
    	return df.format(new Date());
    }
    
    
    /**
     * 按照模式  获取指定日期的字符串形式 如输入yyyy-MM-dd 获取日期为2007-05-22
     * @param pattern
     * @return
     */
    public static String getFormatDate(String pattern, Date date){
    	DateFormat df=new SimpleDateFormat(pattern);
    	return df.format(date);
    }
    
    /**
     * 将日期从一种形式 转换成另一种形式 
     * @param outPattern  日期的输出形式
     * @param inputPattern 日期的输入形式
     * @param inputString  日期的字符串表达式
     * @return
     */
    public static String getFormatDate(String outPattern, String inputPattern, String inputString){
    	
    	if(inputPattern.length()!=inputString.length()){
    		//throw new SandException("输入的字符串格式不对");
    	}
    	
    	DateFormat df = new SimpleDateFormat(inputPattern);
    	
		Date date=null;
		try {
			date = df.parse(inputString);
		} catch (ParseException e) {
			//throw new SandException("输入的字符串格式不对");
		}
		df=new SimpleDateFormat(outPattern);
		return df.format(date);
    }
	/**
	 * 
	 */
	public static String getChangeDate(String dateStr, String pattern) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		Date date = sf.parse(dateStr);
		
		return getDate(date, pattern);
	}
	public static String getChangeDates(String dateStr, String pattern) throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = sf.parse(dateStr);
		
		return getDate(date, pattern);
	}
	/**
	 * 截取时间14位数
	 * @param v
	 * @return
	 * @throws ParseException
	 */
	public static String getString(String v) throws ParseException {
		
		return v.substring(v.length()-14,v.length());
	}
	
	public static boolean compareV(String d) throws ParseException {
		String currentTime = getDate("yyyyMMddHHmmss");
		if(d.length()>14){
			d=getString(d);
		}
		return Long.parseLong(d)-Long.parseLong(currentTime)>0;
	}
	/**
	 * 把期次的时间转换一下
	 * @param v
	 * @return
	 * @throws ParseException
	 */
	public static String getlottDate(String v) throws ParseException {
		
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
		if(v.length()>14){
			v=getString(v);
		}
		java.util.Date date1 = myFormatter.parse(v);

		return getFormatDate("yyyy-MM-dd HH:mm:ss",date1);
	}
	public static void main(String[]as)  throws Exception{
		long sum =Long.parseLong("1429068600000");
		int a =(int) (sum/1000);
		System.err.println(parentSimple(a, "yyyy-MM-dd hh:mm:ss"));
		
		
		
		/*System.err.println(getTimeSimple());
		//1429061060  1429061453
		System.err.println(1429061060-getTimeSimple());
		
		System.err.println(StringUtil.parentSimple(getTimeSimple(), "yyyy-MM-dd"));*/
		
	}
    
    public static String dealTagOptionCode(String optionCode) throws Exception {
        String result = optionCode;
        if (null != optionCode && !"".equals(optionCode)) {
            if (optionCode.length() % 2 != 0) {
                result = "0" + result;
            }
        }
        return result;
    }
    /**
     * 
     * @param argDate
     * @param patten
     * @return
     * @throws Exception
     */
    public static String getDateByArg(String argDate, String patten) throws Exception{
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = sf1.parse(argDate);
		//
		SimpleDateFormat sf2 = new SimpleDateFormat(patten);
		return sf2.format(date);
	}
    
    /**
     * 从数据库取时间1
     * YYYYMMDDhh24miss
     * @param argDate
     * @param patten
     * @return
     * @throws Exception
     */
    public static String getDateFromDataBase(HttpServletRequest request, final String patten) throws Exception{
    	ServletContext servletContext = request.getSession().getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        //CommonManageService commonManageService = (CommonManageService)wac.getBean("commonManageService");
       // String date = commonManageService.findDateFromDataBase(request, patten);

		return null;
	}
    /**
     * 从数据库取时间2
     * YYYYMMDDhh24miss
     * @param argDate
     * @param patten
     * @return
     * @throws Exception
     */
    public static String getDateFromDataBase(ServletContextEvent sce, final String patten) throws Exception{
    	ServletContext servletContext = sce.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//        CommonManageService commonManageService = (CommonManageService)wac.getBean("commonManageService");
//        String date = commonManageService.findDateFromDataBase(patten);

		return "";
	}
    
    /**
     * 通过id 从参数表里 取出 系统工作日期
     * @param request
     * @param id
     * @return
     * @throws Exception
     */
    public static String getWorkDateFromSysParam(HttpServletRequest request, final String id) throws Exception{
    	ServletContext servletContext = request.getSession().getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//        SysParamBackService sysParamBackService = (SysParamBackService)wac.getBean("sysParamBackService");
//        SysParam sysParam = sysParamBackService.findSysParamById(id);

//		return sysParam.getValueChar();
        return "";
	}
    
    public final static Pattern moneyStr = Pattern.compile("\\w*[a-zA-Z]\\w*[a-zA-Z]\\w*[a-zA-Z]\\w*[a-zA-Z]\\w*");
    
    /**
     * 判断是否是金额 如0.00
     * 郑梦久
     * @param args
     * @return
     * @throws Exception
     */
    public static boolean isMoney(String args) throws Exception{
    	Pattern pAm = Pattern.compile("^(-?\\d+)(\\.\\d{2})?$");
    	if(pAm.matcher(args).matches()){
		   return true;
	    }
	    
	    return false;
    }
    
    public static String steadyLength(String p_SourStr, String p_AppendStr,
            int p_DestinateLength, String p_AddSign) {
        String returnValue = null;
        try {
            if (p_SourStr != null && p_AppendStr != null
                    && p_DestinateLength != 0 && p_AddSign != null) {
                int sourStrLen = p_SourStr.getBytes().length;
                if (p_AddSign.equals("L")) {
                    for (int i = 0; i < p_DestinateLength - sourStrLen; i++)
                        p_SourStr = p_AppendStr + p_SourStr;
                } else if (p_AddSign.equals("R")) {
                    for (int j = 0; j < p_DestinateLength - sourStrLen; j++)
                        p_SourStr = p_SourStr + p_AppendStr;
                } else if (p_AddSign.equals("M")) {
                    for (int j = 0; j < (p_DestinateLength - sourStrLen) / 2; j++)
                        p_SourStr = p_AppendStr + p_SourStr;
                    for (int j = 0; j < (p_DestinateLength - sourStrLen) / 2; j++)
                        p_SourStr = p_SourStr + p_AppendStr;
                    if (p_SourStr.length() < p_DestinateLength)
                        p_SourStr = p_SourStr + p_AppendStr;
                } else {
                    // System.out.println("参数错误");
                }
                returnValue = p_SourStr;
            } else {
                // System.out.println("参数错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return returnValue;
        }
    }
    
    public static String formatDateString(String strDate, String oldPattern,
            String newPattern) throws Exception {
        String resultDate = "";
        SimpleDateFormat dFormat = new SimpleDateFormat(oldPattern);
        try {
            java.util.Date date = dFormat.parse(strDate);
            dFormat = new SimpleDateFormat(newPattern);
            resultDate = dFormat.format(date);
        } catch (ParseException e) {}
        return resultDate;
    }
    
    /**
     * Description : 返回_ID集合中的每个id加上引号，并以逗号隔开 <br>
     * Created on 2007-5-22 10:14:12 <br>
     * @param idList
     * @return
     */
    public static List getidsForSql(List idList) {
        List rlt = new ArrayList();
        int m = -1;
        if (idList != null && idList.size() > 0) {
            for (int i = 0; i < idList.size(); i += 1000) {
                m++;
                int n = m * 1000;
                int j = n + 999;
                if (j >= idList.size()) {
                    j = idList.size() - 1;
                }
                StringBuffer strBuf = new StringBuffer();
                for (int k = n; k <= j; k++) {
                    strBuf.append((0 == k - n) ? "" : ",");
                    strBuf.append("'").append(idList.get(k).toString().trim()).append("'");
                }
                rlt.add(strBuf.toString());
            }
        } else {
            rlt.add("''");
        }
        return rlt;
    }
    
    public static String legDigit(Class cls, Object p) {
        String rlt = "0";
        try {
            Constructor con = cls.getConstructor(new Class[] { String.class });
            String para = null2Blank(p);
            con.newInstance(para);
            rlt = para;
        } catch (Exception e) {}
        return rlt;
    }
    
    /**
     * Description : 返回校验位 <br>
     * Created on 2009-11-25 下午06:02:32 <br>
     * 
     * @param argBin 卡标识
     * @param argSer 卡序列
     * @return
     */
    public static String checkdigit(String st) {
        String strPrev = st;
//        String strPrev = "980100012000000002";
        int i, j, k, l;
        int e, f, g;
        j = 1;
        g = 0;
        for (i = strPrev.length(); i >= 1; i--) {
            if ((j % 2) == 1) {
                e = 0;
                char m = strPrev.charAt(i - 1);
                int n = Integer.parseInt(String.valueOf(m));
                l = n * 2;
                if (l >= 10)
                    f = (l / 10) + (l % 10);
                else
                    f = l;
            } else {
                char m = strPrev.charAt(i - 1);
                int n = Integer.parseInt(String.valueOf(m));
                e = n;
                f = 0;
            }
            g += e + f;
            j++;
        }
        k = (10 - (g % 10)) % 10;
        return String.valueOf(k);
    }
    
    public static char[] getCardNo(int cardLen, String strBin, int binOffset,
            int binLen, String strSer, int serOffset, int serLen) {
        char[] rlt = new char[cardLen];
        char[] binAry = strBin.toCharArray();
        for (int i = 0, j = binOffset - 1; i < binLen; i++, j++) {
            rlt[j] = binAry[i];
        }
        char[] serAry = strSer.toCharArray();
        for (int i = 0, j = serOffset - 1; i < serLen; i++, j++) {
            rlt[j] = serAry[i];
        }
        return rlt;
    }
    

    /**
     * 生成不重复的随机数
     * 
     * @return
     */
    public static Integer getRandom()	//生成随机数
	{
    	//生成 [0-n) 个不重复的随机数
        //list 用来保存这些随机数
        ArrayList list = new ArrayList();
        int n = 1;
        Random rand = new Random();
        boolean[] bool = new boolean[n];        
        int num =0;        
        for (int i = 0; i<n; i++){            
            do{
                //如果产生的数相同继续循环
                num = rand.nextInt(n);    
            }while(bool[num]);
            bool[num] =true;
            list.add(num);        
        }           

		return (Integer)list.get(0);
	}
    
    
    /**
     * 获取ID
     * @return
     */
    public synchronized static String  getLocalSerial(){
    	String result =getDate("hhmmss")+StringUtil.fillString(order+"","0",8,0);
		return result;
    }
    
    public synchronized static String genUUID(String id){
    	order++;
    	return id+StringUtil.getDate("yyyyMMddhhmmss")+StringUtil.getRandom()+order;
    	
    }
    
    //把页面创建的日期string格式转换为我们要求的格式yyyyMMddHHmmss
    public static String getStrDate(String str){
    	String backDate = "";
    	try {
    		if(str.split(" ").length == 7){
        		String newdate1 = str.substring(str.indexOf("UTC"), str.indexOf("0800")).replaceAll(" ", "+");
        		String lis = str.substring(0,str.indexOf("UTC"))+newdate1+str.substring(str.indexOf("0800"));
        		DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'UTC'Z yyyy",Locale.ENGLISH);
        		Date date = df.parse(lis);
        		backDate = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        	}
		} catch (Exception e) {
			// TODO: handle exception
		}
    	
    	return backDate;
    }

  

}
